// hotel-management-frontend/src/services/api.js

import axios from "axios";

const API_BASE_URL = "http://localhost:8081/api"; // 后端API的基础URL

// 创建Admin API客户端
const adminAxios = axios.create({
  baseURL: API_BASE_URL + "/admin",
});

// 创建Client API客户端
const clientAxios = axios.create({
  baseURL: API_BASE_URL + "/client",
});

export const adminAPI = {
  // 房间管理
  addRoom: (roomData) => adminAxios.post("/rooms", roomData),
  getAllRooms: (params) => adminAxios.get("/rooms", { params }),
  getAvailableRooms: () => adminAxios.get("/rooms/available"), // 用于入住下拉选择
  updateRoomStatus: (roomId, status) =>
    adminAxios.put(`/rooms/${roomId}/status`, { status }),
  bulkImportRooms: (data) => adminAxios.post("/rooms/import", data), // 批量导入房间
  // 修正：使用 adminAxios 并且路径不需要再拼接 API_BASE_URL
  updateRoom: (roomId, roomData) =>
    adminAxios.put(`/rooms/${roomId}`, roomData),
  // 修正：使用 adminAxios 并且路径不需要再拼接 API_BASE_URL
  deleteRoom: (roomId) => adminAxios.delete(`/rooms/${roomId}`),
  // 客户管理
  addGuest: (guestData) => adminAxios.post("/guests", guestData),
  searchGuests: (params) => adminAxios.get("/guests", { params }),
  updateGuest: (id, guestData) => adminAxios.put(`/guests/${id}`, guestData),
  deleteGuest: (id) => adminAxios.delete(`/guests/${id}`),
  getAllGuests: () => adminAxios.get("/guests"),

  // 入住管理
  createCheckin: (checkinData) => adminAxios.post("/checkins", checkinData),
  getAllCheckins: (params) => adminAxios.get("/checkins", { params }),
  getCheckinById: (id) => adminAxios.get(`/checkins/${id}`),
  // 退房功能：注意这里是 checkoutGuest, 对应后端路由的PUT请求
  checkoutGuest: (checkinId, checkoutData) =>
    adminAxios.put(`/checkins/${checkinId}/checkout`, checkoutData),

  // 消费管理 (管理员可以添加/查看/删除所有消费)
  addConsumption: (consumptionData) =>
    adminAxios.post("/consumptions", consumptionData),
  getConsumptionsByCheckin: (checkinId) =>
    adminAxios.get(`/consumptions/${checkinId}`), // 管理员查看某个入住的消费明细
  deleteConsumption: (consumptionId) =>
    adminAxios.delete(`/consumptions/${consumptionId}`), // 删除消费记录

  // 统计与仪表盘 (仅管理员)
  getDashboardOverview: () => adminAxios.get("/dashboard/overview"),
  getRoomStats: (params) => adminAxios.get("/stats/rooms", { params }),
  getConsumptionStats: (params) =>
    adminAxios.get("/stats/consumptions", { params }),

  // 商品管理 (仅管理员)
  getAllMenuItems: () => adminAxios.get("/menuitems"),
  addMenuItem: (itemData) => adminAxios.post("/menuitems", itemData),
  updateMenuItem: (itemId, itemData) =>
    adminAxios.put(`/menuitems/${itemId}`, itemData),
  deleteMenuItem: (itemId) => adminAxios.delete(`/menuitems/${itemId}`),
  bulkImportMenuItems: (data) => adminAxios.post("/menuitems/import", data), // 修正了原本的 formData 错误
};

export const clientAPI = {
  // 客户API调用 (根据身份证查询)
  getGuestByCard: (idCard) => clientAxios.get(`/guests/${idCard}`),
  getCheckinsByGuestCard: (idCard) => clientAxios.get(`/checkins/${idCard}`),
  // 客户端查询某个入住单号的消费记录 (ClientHistory.js 中的展开行会用到)
  getConsumptionsByCheckinId: (checkinId) =>
    clientAxios.get(`/consumptions/${checkinId}`),
  // 获取所有菜单项，用于客户点餐
  getAllMenuItems: () => clientAxios.get("/menuitems"), // 客户也需要获取菜单项
  // 客户端添加消费 (如果需要客户端直接添加的入口)
  addClientConsumption: (checkinId, consumptionData) =>
    clientAxios.post(`/checkins/${checkinId}/consumptions`, consumptionData),
};
